# 1、list：有序，不唯一，可变
# 2、tuple: 有序，不唯一,不可变
# 3、字典：kv结构,key唯一
# 4、set: 无序，唯一

# 1、list
l = [1, 2, 3, 23, 4, 5, 6, 7, 8, 9, 100]
print(l)
# 追加元素
l.append(10)
print(l)
# 排序
l.sort(reverse=True)
print(l)
# 反转
l.reverse()
print(l)
# 插入
l.insert(2, 100)
print(l)
# 删除第一次出现的元素
l.remove(100)
print(l)
# 通过下标取数据
print(l[0])
print(l[len(l) - 1])
# 使用负下标
print(l[-1])
# 删除最后一个元素
del l[-1]
print(l)

# 切片,l[开始位置:结束位置:步长]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, "100"]
print(l[3:6:1])
print(l[0:9:2])
# 开始位置(默认值是从头开始):结束位置(默认值是到末尾):步长(默认值是1)
print(l[::2])
# 使用负下标
print(l[-3::])
# 当步长为负数时，从右向左切片
print(l[::-1])
print(l)

# 2、元组
t = (1, 2, 3, 4, 5, 6, 7)
print(t, type(t))

info = ("001", "张三", 23)
print(info[0])
print(info[1])
print(info[2])

id, name, age = info
print(id)
print(name)
print(age)

# 3、字典
names = {"001": "张三", "002": "李四"}
print(names, type(names))
# 通过key获取value
# 如果key不存在会报错
print(names["001"])
# 如果key不存在返回None
print(names.get("004"))

# 更新或者新增元素
names["003"] = "张三1"
print(names)

# 删除元素
del names["003"]
print(names)

print(names.keys())
print(names.values())

# 循环字典
for id in names:
    print(id, names[id])

print(names.items())
for id, name in names.items():
    print(id, name)

# 清空字典
names.clear()
print(names)

# 4、set
s = {111, 1, 2, 1, 12, 1231, 23, 124, 1, 1, 2, 2, 1, 2, 34, 3}
print(s)
s.add(1000)
print(s)

l = [1, 2, 1, 1, 2, 12, 2, 2, 2]
# 类型转换
print(set(l))

# 交集，并集，差集
s1 = {1, 2, 3, 4, 5, 6, 7}
s2 = {4, 5, 6, 7, 8, 9, 10}
print(s1 & s2)  # 交集
print(s1 | s2)  # 并集
print(s1 - s2)  # 差集

# 列表生成式
print(list(range(1, 10)))

for i in range(10):
    print(i)

print("=" * 100)

l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(l)
# enumerate:带下标循环
for index, i in enumerate(l):
    l[index] = i * 2
print(l)

# 列表推导式(类似scala中的map方法)
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
new_list = [i * 2 for i in l]
print(new_list)

# 打印九九乘法表
for i in range(1, 10):
    for j in range(1, i + 1):
        print(f"{j} * {i} = {j * i}", end="\t")
    print()

s = "java,spark,scala,python"
words = s.split(",")
print(words)
# join：将字符串类型列表拼接成字符串
print("|".join(words))

print("\n".join(["\t".join([f"{j} * {i} = {j * i}" for j in range(1, i + 1)]) for i in range(1, 10)]))